<?php

namespace frontend\models;

/**
 * @author  shuang
 * @date    2016-10-25 9:01:48
 * @version V1.0
 * @desc    
 */
use common\models\Task;
use Yii;
use common\components\MTools;
use yii\helpers\ArrayHelper;
use yii\behaviors\TimestampBehavior;
use yii\web\UploadedFile;

class WB_Task extends Task {
    /*
     * 设置表操作行为动作
     * return array
     */

    public function behaviors() {
        return[
            [
                'class' => TimestampBehavior::className()
            ]
        ];
    }

    public function rules() {
        return ArrayHelper::merge(parent::rules(), [
                    ["sample_figure", "required", "message" => "请上传样图"],
                    [['sample_figure_count'], 'compare', 'compareValue' => 0, 'operator' => '>', "message" => "亲，您还没有选择所需截图数量呢！"]
        ]);
    }

    /*
     * 发布任务
     */

    public static function createData($id = null) {
        $model = $id ? WB_Task::findOne($id) : new WB_Task();
        $model->userid = Yii::$app->user->id;
        $model->type = Yii::$app->request->post("type");
        $model->publicode = Yii::$app->request->post("publicode");
        $model->title = Yii::$app->request->post("title");
        $model->vote_address = Yii::$app->request->post("vote_address");
        $model->task_price_id = Yii::$app->request->post("task_price_id");
        $model->number = Yii::$app->request->post("number");
        $model->single_meet = Yii::$app->request->post("single_meet");
        $model->note = Yii::$app->request->post("note");
        $model->evaluation = Yii::$app->request->post("evaluation");
        $model->ordersenddate = Yii::$app->request->post("ordersenddate") ? strtotime(Yii::$app->request->post("ordersenddate")) : time();
        $model->end_at = Yii::$app->request->post("end_at") ? strtotime(Yii::$app->request->post("end_at")) : null;
        $model->is_speed = Yii::$app->request->post("is_speed");
        $model->speed_limit = Yii::$app->request->post("speed_limit");
        $model->sample_figure = Yii::$app->request->post("sample_figure");
        $model->sample_figure_count = Yii::$app->request->post("sample_figure_count");
        $model->qrcode = Yii::$app->request->post("qrcode");
        $model->description = Yii::$app->request->post("description");
        $model->areaname = Yii::$app->request->post("areaname");
        $model->is_offer_mobile = Yii::$app->request->post("is_offer_mobile");
        $model->considerations = Yii::$app->request->post("considerations");
//        $model->step_01 = Yii::$app->request->post("step");
//        $model->step_02 = Yii::$app->request->post("step_02");
//        $model->step_03 = Yii::$app->request->post("step_03");
        $stepcount = Yii::$app->request->post("stepcount");
        $steptemp = [];
        for($i = 0; $i <= $stepcount; $i++){
          if(Yii::$app->request->post("step".$i) || isset($_FILES['stepfile'.$i])){
              array_push($steptemp,[
                  'step'=>Yii::$app->request->post("step".$i),
                  'stepfile'=>isset($_FILES['stepfile'.$i]) ? MTools::stepMoreImage($_FILES['stepfile'.$i]) : ''
              ]);
          }  
        }
        $model->step = \yii\helpers\Json::encode($steptemp);
        $model->reflash_at = time();
        $model->status = 67;
        $sample_figure = UploadedFile::getInstance($model, "sample_figure");
        if ($sample_figure) {
            $model->sample_figure = MTools::upQiniu($sample_figure->tempName);
        }
        $qrcode = UploadedFile::getInstance($model, "qrcode");
        if ($qrcode) {
            $model->qrcode = MTools::upQiniu($qrcode->tempName);
        }
        return MTools::saveModel($model);
    }

    /*
     * 任务列表
     * @params $status
     * @params $title
     * return array
     */

    public static function getList($status, $title) {
        $query = WB_Task::find()->with(["statusID"])->orderBy("created_at desc");
        if ($status > 0) {
            $query->where("status=$status");
        }
        if ($title) {
            $query->where("title='%$title%'");
        }
        $countQuery = clone $query;
        $pager = new \yii\data\Pagination(['totalCount' => $countQuery->count(), 'defaultPageSize' => 10]);
        $offset = Yii::$app->request->get("offset");
        $limit = Yii::$app->request->get("limit");
        $res = $query->offset($offset)->limit($limit)->asArray()->all();
        return ["pager" => $pager, "data" => $res];
    }

    /*
     * 关联字典表  任务状态
     */

    public function getStatusID() {

        return $this->hasOne(WB_Dictionary::className(), ['id' => 'status']);
    }

    /*
     * 关联单价
     */

    public function getPriceID() {
        return $this->hasOne(WB_TaskPrice::className(), ['id' => 'task_price_id']);
    }

    /*
     * 编辑任务
     */

    public static function editTask($id) {
        $model = WB_Task::findOne($id);
        $model->userid = Yii::$app->user->id;
        $model->publicode = Yii::$app->request->post("publicode") ? Yii::$app->request->post("publicode") : $model->publicode;
        $model->vote_address = Yii::$app->request->post("vote_address");
        $model->note = Yii::$app->request->post("note");
        $model->evaluation = Yii::$app->request->post("evaluation") ? Yii::$app->request->post("evaluation") : $model->evaluation;
        $model->description = Yii::$app->request->post("description");
        $model->title = Yii::$app->request->post("title");
        $model->sample_figure_count = Yii::$app->request->post("sample_figure_count");
        $model->considerations = Yii::$app->request->post("considerations");
        $stepcount = Yii::$app->request->post("stepcount");
        $steptemp = [];
        for($i = 0; $i <= $stepcount; $i++){
          if(Yii::$app->request->post("step".$i) || Yii::$app->request->post("stepfile".$i) || isset($_FILES['stepfile'.$i])){
              array_push($steptemp,[
                  "step"=>Yii::$app->request->post("step".$i),
                  "stepfile"=>isset($_FILES['stepfile'.$i]) ? MTools::stepMoreImage($_FILES['stepfile'.$i]) : (Yii::$app->request->post("stepfile".$i) ? Yii::$app->request->post("stepfile".$i) : "")
              ]);
          }  
        }
        $model->step = \yii\helpers\Json::encode($steptemp);
//        $model->step_01 = Yii::$app->request->post("step_01");
//        $model->step_02 = Yii::$app->request->post("step_02");
//        $model->step_03 = Yii::$app->request->post("step_03");
        $sample_figure = UploadedFile::getInstance($model, "sample_figure");
        if ($sample_figure) {
            $model->sample_figure = MTools::upQiniu($sample_figure->tempName);
        }
        $qrcode = UploadedFile::getInstance($model, "qrcode");
        if ($qrcode) {
            $model->qrcode = MTools::upQiniu($qrcode->tempName);
        }
        return MTools::saveModel($model);
    }

    /*
     * 后台编辑任务
     */

    public static function editTask2($id) {
        $model = WB_Task::findOne($id);
        $model->publicode = Yii::$app->request->post("publicode") ? Yii::$app->request->post("publicode") : $model->publicode;
        $model->vote_address = Yii::$app->request->post("vote_address");
        $model->note = Yii::$app->request->post("note");
        $model->evaluation = Yii::$app->request->post("evaluation") ? Yii::$app->request->post("evaluation") : $model->evaluation;
        $model->description = Yii::$app->request->post("description");
        $model->title = Yii::$app->request->post("title");
        $model->sample_figure_count = Yii::$app->request->post("sample_figure_count");
        $model->considerations = Yii::$app->request->post("considerations");
        $model->step_01 = Yii::$app->request->post("step_01");
        $model->step_02 = Yii::$app->request->post("step_02");
        $model->step_03 = Yii::$app->request->post("step_03");
        $sample_figure = UploadedFile::getInstance($model, "sample_figure");
        if ($sample_figure) {
            $model->sample_figure = MTools::upQiniu($sample_figure->tempName);
        }
        $qrcode = UploadedFile::getInstance($model, "qrcode");
        if ($qrcode) {
            $model->qrcode = MTools::upQiniu($qrcode->tempName);
        }
        return MTools::saveModel($model);
    }

    public static function getUserTaskCount($userid) {
        return WB_Task::find()->where("userid=:userid", [":userid" => $userid])->count();
    }

}
